using System;
using Controls;
using Events;
using UI;
using UnityEngine;

namespace Globals.States
{
    public class PauseState : MonoBehaviour, IState
    {
        [SerializeField] private VoidEventChannel pausedEvent;
        [SerializeField] private PlayerController playerController;
        [SerializeField] private GameplayUi gameplayUi;


        private void Start()
        {
            pausedEvent.Init();
        }

        public void Enter()
        {
            playerController.PausePerformed += PlayerController_PausePerformed;
            gameplayUi.TogglePauseUI();
            
            Time.timeScale = 0f;
            
            pausedEvent.Raise();
        }

        public void OnUpdate()
        {
        }

        public void Exit()
        {
            playerController.PausePerformed -= PlayerController_PausePerformed;

            if (gameplayUi)
            {
                gameplayUi.TogglePauseUI();
            }
            
            Time.timeScale = 1f;
        }
        
        private void PlayerController_PausePerformed()
        {
            GameState.Instance.SwitchToPreviousState();
        }
    }
}
